# -*- coding:utf-8 -*-
from heuristics.Pile_Division import *
from heuristics.Manhattan_Obst import *
from search.all import *
from multi_robot_problem import MultiRobotState
from time import *
from generate_output import *
from problem_generator import ProblemGenerator
"""
Created on Apr 11, 2011

@author: bob
"""

' algorithms initialization '
algo_gbf_any = BestFirstAnyTimeGraphSearch()
algo_astar_any_2 = AStarAnytime2()

num_of_problems = 15
time1 = 10
time2 = 20
time3 = 30

filename_astar_man = "C:\\Users\\bob\\Desktop\\ai\\output_astar_man.txt"
filename_astar_pile = "C:\\Users\\bob\\Desktop\\ai\\output_astar_pile.txt"
filename_gbf_man = "C:\\Users\\bob\\Desktop\\ai\\output_gbf_man.txt"
filename_gbf_pile = "C:\\Users\\bob\\Desktop\\ai\\output_gbf_pile.txt"
filename = "C:\\Users\\bob\\Desktop\\ai\\problems.txt"
'initializing random problems generator'

height, width = 10,10
minRobs, maxRobs = 4,4
difficulty = ProblemGenerator.HARD
minPiles, maxPiles, minObs, maxObs = 6, 6, 3, 4 
minWallLength, maxWallLength = 5,7

pGen = ProblemGenerator(height, width, minRobs, maxRobs, difficulty,
    minPiles, maxPiles, minObs, maxObs, minWallLength, maxWallLength)

print "Running started."
hours = 4*(time1+time2+time3)*num_of_problems/3600
minutes = (4*(time1+time2+time3)*num_of_problems/60) %60
seconds = (4*(time1+time2+time3)*num_of_problems) % 60
print "expected running time:",hours,":",minutes,":",seconds


file_astar_man = open(filename_astar_man,"a")
file_astar_pile = open(filename_astar_pile,"a")
file_gbf_man = open(filename_gbf_man,"a")
file_gbf_pile = open(filename_gbf_pile,"a")

file_astar_man.write("FOUND?\tlength\tsols\tFirst solution time\n")
file_astar_pile.write("FOUND?\tlength\tsols\tFirst solution time\n")
file_gbf_man.write("FOUND?\tlength\tsols\tFirst solution time\n")
file_gbf_pile.write("FOUND?\tlength\tsols\tFirst solution time\n")

file_astar_man.close()
file_astar_pile.close()
file_gbf_man.close()
file_gbf_pile.close()

start_time = clock()
for x in xrange(0,num_of_problems):
    print "Computing problem", x+1, "out of", num_of_problems
    problem = pGen.trollProblem()
    
    FILE = open(filename,"a")
    FILE.write("\tProblem #"+str(x)+":"+str(asctime())+"\n")
    FILE.write("size:"+str((problem.width,problem.height))+"\n")
    FILE.write("robots:"+str(problem.robots)+"\n")
    FILE.write("obstacles:"+str(problem.obstacle_locations)+"\n")
    FILE.write("piles:"+str(problem.dirt_locations)+"\n")
    FILE.close()
    
    print problem
    file_astar_man = open(filename_astar_man,"a")
    file_astar_pile = open(filename_astar_pile,"a")
    file_gbf_man = open(filename_gbf_man,"a")
    file_gbf_pile = open(filename_gbf_pile,"a")
    
    'calculate for astar and manhattan'
    file_astar_man.write("problem number "+str(x+1)+" :\n")
    generate_output(file_astar_man, problem, algo_astar_any_2, Manhattan_Obst, time1)
    generate_output(file_astar_man, problem, algo_astar_any_2, Manhattan_Obst, time2)
    generate_output(file_astar_man, problem, algo_astar_any_2, Manhattan_Obst, time3)
 
    'calculate for astar and pile'
    file_astar_pile.write("problem number "+str(x+1)+" :\n")
    generate_output(file_astar_pile, problem, algo_astar_any_2, Pile_Division, time1)
    generate_output(file_astar_pile, problem, algo_astar_any_2, Pile_Division, time2)
    generate_output(file_astar_pile, problem, algo_astar_any_2, Pile_Division, time3)

    'calculate for gbf and manhattan'
    file_gbf_man.write("problem number "+str(x+1)+" :\n")
    generate_output(file_gbf_man, problem, algo_gbf_any, Manhattan_Obst, time1)
    generate_output(file_gbf_man, problem, algo_gbf_any, Manhattan_Obst, time2)
    generate_output(file_gbf_man, problem, algo_gbf_any, Manhattan_Obst, time3)
    
    'calculate for gbf and pile'
    file_gbf_pile.write("problem number "+str(x+1)+" :\n")
    generate_output(file_gbf_pile, problem, algo_gbf_any, Pile_Division, time1)
    generate_output(file_gbf_pile, problem, algo_gbf_any, Pile_Division, time2)
    generate_output(file_gbf_pile, problem, algo_gbf_any, Pile_Division, time3)

    file_astar_man.close()
    file_astar_pile.close()
    file_gbf_man.close()
    file_gbf_pile.close()
    
print "~~ Running finished. "
#print "Running time =", clock()-start_time

